Skip to main content

class AttrCacheTable

The class AttrCacheTable is used to cache Attribute Catalog entries of the attributes of open relations in NITCbase. The first two entries of the Attribute Cache Table corresponding to RELCAT_RELID and ATTRCAT_RELID are reserved for storing the entries of Relation Catalog relation and Attribute Catalog relation, respectively. These are loaded into the cache by the OpenRelTable constructor at the start of the session. These relations remain in the cache memory throughout the session and can only be closed by the OpenRelTable destructor during shutdown.

The class contains a private member field, attrCache, which is an array of pointers to struct AttrCacheEntry with size MAX_OPEN. For each relation opened, an entry is made in the array attrCache, at the index given by relation id of the relation. This entry is the head of the linked list of struct AttrCacheEntry elements. A linked list is used because a relation can have variable number of attributes (though the maximum number of attributes for a relation is bounded in Nitcbase by 125 - why?). Each element in the linked list corresponds to an attribute of the relation.

The class provides public overloaded methods - getAttrCatEntry() and setAttrCatEntry() to retrieve and update the Attribute Catalog entry of a relation's attribute in the Attribute Cache Table. The class also provides overloaded public methods - getSearchIndex() and setSearchIndex() for retrieving and updating the searchIndex field of Attribute Cache Entry.

The private method recordToAttrCatEntry() is used to convert a record (implemented as an array of union Attribute) to AttrCatEntry structure. This function is called by the friend class, OpenRelTable, while opening a relation. Similarly, the private method attrCatEntryToRecord() is used to convert AttrCatEntry structure in to a record. This function is also called from the friend class, OpenRelTable, while closing a relation.

C++ STATIC CLASSES

AttrCacheTable is a static class, i.e., all member fields and methods are declared static. Memory is allocated statically for all member fields of the class. This class uses static methods to access the static member fields. C++ allows static methods to be accessed using the semantics class_name :: function_name().

Note

The class OpenRelTable is a friend class to AttrCacheTable class. This allows all methods in OpenRelTable to access the private fields and methods of the AttrCacheTable class.

The class definition of AttrCacheTable is as given below.

class AttrCacheTable {

friend class OpenRelTable;

public:
//methods
static int getAttrCatEntry(int relId, char attrName[ATTR_SIZE], AttrCatEntry *attrCatBuf);
static int getAttrCatEntry(int relId, int attrOffset, AttrCatEntry *attrCatBuf);
static int setAttrCatEntry(int relId, char attrName[ATTR_SIZE], AttrCatEntry *attrCatBuf);
static int setAttrCatEntry(int relId, int attrOffset, AttrCatEntry *attrCatBuf);
static int getSearchIndex(int relId, char attrName[ATTR_SIZE], IndexId *searchIndex);
static int getSearchIndex(int relId, int attrOffset, IndexId *searchIndex);
static int setSearchIndex(int relId, char attrName[ATTR_SIZE], IndexId *searchIndex);
static int setSearchIndex(int relId, int attrOffset, IndexId *searchIndex);

private:
//field
static AttrCacheEntry* attrCache[MAX_OPEN];

//methods
static void recordToAttrCatEntry(union Attribute record[ATTRCAT_NO_ATTRS], AttrCatEntry *attrCatEntry);
static void attrCatEntryToRecord(AttrCatEntry *attrCatEntry, union Attribute record[ATTRCAT_NO_ATTRS]);

};

The following are the specifications for the methods in class AttrCacheTable.

AttrCacheTable :: getAttrCatEntry

Description

Gives the Attribute Catalog entry corresponding to the given attribute of the specified relation in the Attribute Cache Table.

note
  • The caller should allocate memory for the struct AttrCatEntry before calling the function.
  • This method is overloaded in type of the second argument.

Arguments

NameTypeDescription
relIdintThe relation id of the relation in the Attribute Cache Table
attrName / attrOffsetchar[ATTR_SIZE] / intThe name/offset of the target attribute
attrCatBufAttrCatEntry*Pointer to struct AttrCatEntry to which the Attribute Catalog entry corresponding to the input relid and attribute is to be copied

Return Values

ValueDescription
SUCCESSSuccessfully copied the Attribute Catalog entry
E_OUTOFBOUNDInput relId is outside the valid set of possible relation ids
E_RELNOTOPENEntry corresponding to input relId is free in the Attribute Cache Table.
E_ATTRNOTEXISTNo attribute with the input attribute name or offset exists

Algorithm

int AttrCacheTable::getAttrCatEntry(int relId, char attrName[ATTR_SIZE]/int attrOffset, AttrCatEntry *attrCatBuf) {

if(/*relId is outside the range [0, MAX_OPEN-1]*/) {
return E_OUTOFBOUND;
}

if(/*entry corresponding to the relId in the Attribute Cache Table is free*/) {
return E_RELNOTOPEN;
}

for(/* each attribute corresponding to relation with relId */)
{
if (/* attrName/offset field of the AttrCatEntry
is equal to the input attrName/attrOffset */)
{
// copy that Attribute Catalog entry in the Attribute Cache Table to
// attrCatBuf.

return SUCCESS;
}
}

return E_ATTRNOTEXIST;
}

AttrCacheTable :: setAttrCatEntry

Description

Sets the Attribute Catalog entry corresponding to the given attribute of the specified relation in the Attribute Cache Table.

note
  • The caller should allocate memory to the pointer to struct AttrCatEntry before calling the function.
  • This method is overloaded in type of the second argument.

Arguments

NameTypeDescription
relIdintThe relation id of the relation in the Attribute Cache Table
attrName / attrOffsetchar[ATTR_SIZE] / intThe name/offset of the target attribute
attrCatBufAttrCatEntry*Pointer to struct AttrCatEntry using which the Attribute Catalog entry corresponding to input relId and attribute is to be updated

Return Values

ValueDescription
SUCCESSSuccessfully copied the Attribute Catalog entry
E_OUTOFBOUNDInput relId is outside the valid set of possible relation ids
E_RELNOTOPENEntry corresponding to input relId is free in the Attribute Cache Table.
E_ATTRNOTEXISTNo attribute with the input attribute name or offset exists

Algorithm

int AttrCacheTable::setAttrCatEntry(int relId, char attrName[ATTR_SIZE]/int attrOffset, AttrCatEntry *attrCatBuf) {

if(/*relId is outside the range [0, MAX_OPEN-1]*/) {
return E_OUTOFBOUND;
}

if(/*entry corresponding to the relId in the Attribute Cache Table is free*/) {
return E_RELNOTOPEN;
}

for(/* each attribute corresponding to relation with relId */)
{
if(/* the attrName/offset field of the AttrCatEntry
is equal to the input attrName/attrOffset */)
{
// copy the attrCatBuf to the corresponding Attribute Catalog entry in
// the Attribute Cache Table.

// set the dirty flag of the corresponding Attribute Cache entry in the
// Attribute Cache Table.

return SUCCESS;
}
}

return E_ATTRNOTEXIST;
}

AttrCacheTable :: getSearchIndex

Description

Gives the value of searchIndex field of the given attribute in the specified relation from Attribute Cache Table. This is used by the B+ Tree search algorithm to find the location of the previous hit so that the search can be resumed from the next leaf index entry.

note
  • This method is overloaded in type of the second argument.
  • The caller should allocate memory for the struct IndexId before calling the function.

Arguments

NameTypeDescription
relIdintThe relation id of the relation in the Attribute Cache Table
attrName / attrOffsetchar[ATTR_SIZE] / intThe name/offset of the target attribute
searchIndexIndexId*Pointer to struct IndexId to which the searchIndex field of the Attribute Cache entry corresponding to the input relid and attribute is to be copied

Return Values

ValueDescription
SUCCESSSuccessfully copied the search index to the argument searchIndex.
E_OUTOFBOUNDInput relId is outside the valid set of possible relation ids
E_RELNOTOPENEntry corresponding to input relId is free in the Attribute Cache Table.
E_ATTRNOTEXISTNo attribute with the input attribute name or offset exists

Algorithm

int AttrCacheTable::getSearchIndex(int relId, char attrName[ATTR_SIZE]/int attrOffset, IndexId *searchIndex) {

if(/*relId is outside the range [0, MAX_OPEN-1]*/) {
return E_OUTOFBOUND;
}

if(/*entry corresponding to the relId in the Attribute Cache Table is free*/) {
return E_RELNOTOPEN;
}

for(/* each attribute corresponding to relation with relId */)
{
if (/* attrName/offset field of the AttrCatEntry
is equal to the input attrName/attrOffset */)
{
//copy the searchIndex field of the corresponding Attribute Cache entry
//in the Attribute Cache Table to input searchIndex variable.

return SUCCESS;
}
}

return E_ATTRNOTEXIST;
}

AttrCacheTable :: setSearchIndex

Description

Sets the value of searchIndex field of the given attribute in the specified relation's Attribute Cache Table entry. This is used by the B+ Tree search algorithm to set the location of the previous hit so that the search can be resumed from the next leaf index entry.

note
  • This method is overloaded in type of the second argument
  • The value of the search index is expected to be verified by the caller. This function does not check the validity of the search index before setting it into the cache entry.

Arguments

NameTypeDescription
relIdintThe relation id of the relation in the Attribute Cache Table
attrName / attrOffsetchar[ATTR_SIZE] / intThe name/offset of the target attribute
searchIndexIndexId*Pointer to struct IndexId which contains the value to which the searchIndex field is to be updated

Return Values

ValueDescription
SUCCESSSuccessfully set the search index in the attribute cache.
E_OUTOFBOUNDInput relId is outside the valid set of possible relation ids
E_RELNOTOPENEntry corresponding to input relId is free in the Attribute Cache Table.
E_ATTRNOTEXISTNo attribute with the input attribute name or offset exists

Algorithm

int AttrCacheTable::setSearchIndex(int relId, char attrName[ATTR_SIZE]/int attrOffset, IndexId *searchIndex) {

if(/*relId is outside the range [0, MAX_OPEN-1]*/) {
return E_OUTOFBOUND;
}

if(/*entry corresponding to the relId in the Attribute Cache Table is free*/) {
return E_RELNOTOPEN;
}

for(/* each attribute corresponding to relation with relId */)
{
if (/* attrName/offset field of the AttrCatEntry
is equal to the input attrName/attrOffset */)
{
// copy the input searchIndex variable to the searchIndex field of the
//corresponding Attribute Cache entry in the Attribute Cache Table.

return SUCCESS;
}
}

return E_ATTRNOTEXIST;
}

AttrCacheTable :: resetSearchIndex

Description

Resets the value of searchIndex field of the given attribute in the specified relation from Attribute Cache Table to {-1, -1}. This is used so that the B+ tree search can be restarted from the root.

note
  • This method is overloaded in type of the second argument

Arguments

NameTypeDescription
relIdintThe relation id of the relation in the Attribute Cache Table
attrName / attrOffsetchar[ATTR_SIZE] / intThe name/offset of the target attribute

Return Values

ValueDescription
SUCCESSSuccessfully reset the search index in the cache
E_OUTOFBOUNDInput relId is outside the valid set of possible relation ids
E_RELNOTOPENEntry corresponding to input relId is free in the Attribute Cache Table.
E_ATTRNOTEXISTNo attribute with the input attribute name or offset exists

Algorithm

int AttrCacheTable::resetSearchIndex(int relId, char attrName[ATTR_SIZE]/int attrOffset) {

// declare an IndexId having value {-1, -1}
// set the search index to {-1, -1} using AttrCacheTable::setSearchIndex
// return the value returned by setSearchIndex
}

AttrCacheTable :: recordToAttrCatEntry

Description

A utility function that converts a record, implemented as an array of union Attribute, to AttrCatEntry structure. This function can be used to convert the records in Attribute Catalog block/blocks to the corresponding Attribute Cache entries when caching a relation in Attribute Cache Table. The details of the implementation are left to you.

note

The caller should allocate memory for the struct AttrCatEntry and array of union Attribute before calling the function.

Arguments

NameTypeDescription
recordunion Attribute[ATTRCAT_SIZE]The record which is to be converted to an AttrCatEntry.
attrCatEntryAttrCatEntry*Pointer to struct AttrCatEntry to which the contents of the input record is to be copied.

Return Values

Nil

AttrCacheTable :: attrCatEntryToRecord

Description

A utility function that converts AttrCatEntry structure to a record, implemented as an array of union Attribute. This function can be used to convert the Attribute Cache entries to corresponding records that can be written back to Attribute Catalog block/blocks when closing a relation in the cache memory. The details of the implementation are left to you.

note

The caller should allocate memory for the struct AttrCacheEntry and array of union Attribute before calling the function.

Arguments

NameTypeDescription
attrCatEntryAttrCatEntry*Pointer to struct AttrCatEntry which is to be converted to a record.
recordunion Attribute[ATTRCAT_SIZE]The record to which the given AttrCatEntry entry is to be copied.

Return Values

Nil